<template>
  <div class="kong">
    <el-dialog
      :title="isEdit == 1 ? '修改计划' : '新建计划'"
      v-model="dialogVisible"
      width="40%"
      :close-on-click-modal="false"
      :before-close="handelClose"
      center
    >
      <div>
        <el-form ref="rowRef" :rules="rules" :model="row" label-width="120px">
          <el-form-item label="计划类型" prop="planType">
            <el-select
              :value="row.planType"
              v-model="row.planType"
              placeholder="请选择计划类型"
            >
              <el-option
                v-for="item in selectParams"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                default-first-option
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="模块" prop="model">
            <el-input :value="row.model" v-model="row.model"></el-input>
          </el-form-item>
          <el-form-item label="标题" prop="content">
            <el-input :value="row.content" v-model="row.content"></el-input>
          </el-form-item>
          <el-form-item label="详细标题" prop="contentDetail">
            <el-input
              :value="row.contentDetail"
              v-model="row.contentDetail"
            ></el-input>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input :value="row.remark" v-model="row.remark"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="changeStatus(!dialogVisible, rowRef)"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="isEdit == 1 ? submitUpdateIt(rowRef) : submitAddIt(rowRef)"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs, onMounted } from 'vue'
import { updatePlanInfo } from '@/api/scheduleSetting'
import type { planShowVO, rows } from '../scheduleSetting'
import { addPlanInfo } from '@/api/scheduleSetting'
import { getGroupDetail } from '@/api/config'
import { FormRules, FormInstance } from 'element-plus'
import { selectDetail } from '@/constants/commonSelect'

const rowRef = ref<FormInstance>()

const handelClose = (done: () => void, formEl: FormInstance | undefined) => {
  changeStatus(!dialogVisible, formEl)
  done()
}

let planShowVO: planShowVO = reactive({
  nextTime: '',
  sumCount: 0,
  finishCount: 0,
  unfinishCount: 0,
  timeList: [],
})

let row: rows = reactive({
  id: '0',
  model: '',
  content: '',
  contentDetail: '',
  remark: '',
  planType: 1,
  operatorType: 0,
  todayCheckIn: 0,
  planShowVo: planShowVO,
})

interface Props {
  isEdit?: Number
  dialogVisible?: Boolean
  operatorType?: Number
}

// 初始化查询
onMounted(() => {
  getPlanType()
})

const resetFields = (formEl: FormInstance | undefined) => {
  if (formEl) {
    formEl.resetFields()
  } else {
    rowRef.value?.resetFields()
    // row.model = ''
    // row.content = ''
    // row.contentDetail = ''
    // row.remark = ''
    // row.planType = 1
    // row.operatorType = 0
  }
}

const selectParams = ref<selectDetail[]>([])

const getPlanType = async () => {
  getGroupDetail('constgroup_plan_type').then((res: any) => {
    selectParams.value = res
  })
}

const myProps = defineProps<Props>()

const { dialogVisible } = toRefs(myProps)

const emit = defineEmits(['changeDiaLog'])

const changeStatus = (status: boolean, formEl: FormInstance | undefined) => {
  emit('changeDiaLog', status)
  resetFields(formEl)
}

const rules = reactive<FormRules>({
  model: [
    {
      required: true,
      message: '请输入模块',
      trigger: 'change',
    },
  ],
  content: [
    {
      required: true,
      message: '请输入标题',
      trigger: 'change',
    },
  ],
})

const submitAddIt = async (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return
  }
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      row.operatorType = Number(myProps.operatorType)
      addPlanInfo(row).then((res: any) => {
        if (res.data) {
          changeStatus(!dialogVisible, formEl)
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const submitUpdateIt = async (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return
  }
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      updatePlanInfo(row).then((res: any) => {
        if (res.data) {
          changeStatus(!dialogVisible, formEl)
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const initsObj = (rows: rows) => {
  row = rows
}

defineExpose({
  initsObj,
})
</script>

<style lang="scss" scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
